package com.yc.scott.ui;

import java.awt.BorderLayout;
import java.awt.FlowLayout;

import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;

import com.yc.scott.biz.BizException;
import com.yc.scott.biz.EmpBiz;
import com.yc.scott.dao.EmpDao;

import javax.swing.JLabel;
import javax.swing.JOptionPane;

import java.awt.Font;
import javax.swing.JTextField;
import javax.swing.ImageIcon;
import javax.swing.JComboBox;
import javax.swing.JSpinner;
import javax.swing.JPasswordField;
import java.awt.event.ActionListener;
import java.sql.Date;
import java.util.Map;
import java.util.function.Predicate;
import java.awt.event.ActionEvent;
import javax.swing.DefaultComboBoxModel;
import java.math.BigDecimal;

public class EmpEditDlg extends JDialog {

	private static final long serialVersionUID = 1L;
	private final JPanel contentPanel = new JPanel();
	private JTextField textField;
	private JTextField textField_1;
	private JTextField textField_4;
	private JPasswordField passwordField;
	private JPasswordField passwordField_1;
	DBComboBox comboBox;
	DBComboBox comboBox_1;
	DBComboBox comboBox_1_1;
	JSpinner spinner;
	JSpinner spinner_1;
	
	// 修改状态
	private boolean isModify; 

	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		try {
			EmpEditDlg dialog = new EmpEditDlg();
			dialog.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
			dialog.setVisible(true);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	EmpDao dao = new EmpDao();
	public EmpEditDlg(boolean isModify) {
		this();
		this.isModify = isModify;
		textField.setEditable(isModify);
		if(isModify) {
			textField.addActionListener(e->{
				// 读取编号查询员工信息
				Map<String,Object> emp = dao.selectByEmpno(textField.getText());
				textField_1.setText((String) emp.get("ENAME"));
				comboBox.setSelectedItem(Map.of("JOB", emp.get("JOB")));
				textField_4.setText(emp.get("HIREDATE") == null ? "" :emp.get("HIREDATE").toString());
				spinner.setValue(emp.get("SAL")==null? 0: ((BigDecimal)emp.get("SAL")).intValue());
				spinner_1.setValue(emp.get("COMM") == null ? 0 :((BigDecimal)emp.get("COMM")).intValue());
				passwordField.setText((String) emp.get("PWD"));
				passwordField_1.setText((String) emp.get("PWD"));
				
				// lambda表达式写法
				comboBox_1.select(map->map.get("EMPNO").equals(emp.get("MGR")));
				
				// 匿名类写法
				comboBox_1_1.select(new Predicate<Map<String,Object>>() {
					@Override
					public boolean test(Map<String, Object> map) {
						return map.get("DEPTNO").equals(emp.get("DEPTNO"));
					}
				});
				
			});
		}
	}

	/**
	 * Create the dialog.
	 */
	public EmpEditDlg() {
		setModal(true);
		setBounds(100, 100, 501, 476);
		getContentPane().setLayout(new BorderLayout());
		contentPanel.setBorder(new EmptyBorder(5, 5, 5, 5));
		getContentPane().add(contentPanel, BorderLayout.CENTER);
		contentPanel.setLayout(null);
		
		JLabel lblNewLabel = new JLabel("员工信息");
		lblNewLabel.setFont(new Font("宋体", Font.PLAIN, 30));
		lblNewLabel.setBounds(33, 10, 362, 56);
		contentPanel.add(lblNewLabel);
		
		JLabel lblNewLabel_1 = new JLabel("员工编号");
		lblNewLabel_1.setBounds(43, 76, 54, 15);
		contentPanel.add(lblNewLabel_1);
		
		textField = new JTextField();
		textField.setEditable(false);
		textField.setBounds(107, 73, 164, 21);
		contentPanel.add(textField);
		textField.setColumns(10);
		
		JLabel lblNewLabel_1_1 = new JLabel("员工姓名");
		lblNewLabel_1_1.setBounds(43, 104, 54, 15);
		contentPanel.add(lblNewLabel_1_1);
		
		textField_1 = new JTextField();
		textField_1.setColumns(10);
		textField_1.setBounds(107, 101, 164, 21);
		contentPanel.add(textField_1);
		
		JLabel lblNewLabel_1_2 = new JLabel("岗位");
		lblNewLabel_1_2.setBounds(43, 132, 54, 15);
		contentPanel.add(lblNewLabel_1_2);
		
		JLabel lblNewLabel_1_3 = new JLabel("上级领导");
		lblNewLabel_1_3.setBounds(43, 160, 54, 15);
		contentPanel.add(lblNewLabel_1_3);
		
		JLabel lblNewLabel_1_4 = new JLabel("入职时间");
		lblNewLabel_1_4.setBounds(43, 188, 54, 15);
		contentPanel.add(lblNewLabel_1_4);
		
		textField_4 = new JTextField();
		textField_4.setColumns(10);
		textField_4.setBounds(107, 185, 164, 21);
		contentPanel.add(textField_4);
		
		JLabel lblNewLabel_1_5 = new JLabel("工资");
		lblNewLabel_1_5.setBounds(43, 216, 54, 15);
		contentPanel.add(lblNewLabel_1_5);
		
		JLabel lblNewLabel_1_6 = new JLabel("奖金");
		lblNewLabel_1_6.setBounds(43, 244, 54, 15);
		contentPanel.add(lblNewLabel_1_6);
		
		JLabel lblNewLabel_1_7 = new JLabel("部门");
		lblNewLabel_1_7.setBounds(43, 272, 54, 15);
		contentPanel.add(lblNewLabel_1_7);
		
		JLabel lblNewLabel_1_8 = new JLabel("密码");
		lblNewLabel_1_8.setBounds(43, 300, 54, 15);
		contentPanel.add(lblNewLabel_1_8);
		
		JLabel lblNewLabel_1_9 = new JLabel("确认密码");
		lblNewLabel_1_9.setBounds(43, 328, 54, 15);
		contentPanel.add(lblNewLabel_1_9);
		
		JLabel lblNewLabel_2 = new JLabel("");
		lblNewLabel_2.setIcon(new ImageIcon("E:\\who.png"));
		lblNewLabel_2.setBounds(293, 76, 147, 184);
		contentPanel.add(lblNewLabel_2);
		
		JButton btnNewButton = new JButton("选择头像");
		btnNewButton.setBounds(291, 268, 149, 23);
		contentPanel.add(btnNewButton);
		
		comboBox = new DBComboBox("select distinct job from emp", "job");
//		comboBox.setModel(new DefaultComboBoxModel(new String[] {"A", "B", "C"}));
		comboBox.setBounds(107, 128, 164, 23);
		contentPanel.add(comboBox);
		
		comboBox_1 = new DBComboBox("select * from emp", "ename");
		comboBox_1.setBounds(107, 156, 164, 23);
		contentPanel.add(comboBox_1);
		
		spinner = new JSpinner();
		spinner.setBounds(107, 213, 164, 22);
		contentPanel.add(spinner);
		
		spinner_1 = new JSpinner();
		spinner_1.setBounds(107, 241, 164, 22);
		contentPanel.add(spinner_1);
		
		comboBox_1_1 = new DBComboBox("select * from dept", "dname");
		comboBox_1_1.setBounds(107, 268, 164, 23);
		contentPanel.add(comboBox_1_1);
		
		passwordField = new JPasswordField();
		passwordField.setBounds(107, 297, 164, 21);
		contentPanel.add(passwordField);
		
		passwordField_1 = new JPasswordField();
		passwordField_1.setBounds(107, 325, 164, 21);
		contentPanel.add(passwordField_1);
		{
			JPanel buttonPane = new JPanel();
			buttonPane.setLayout(new FlowLayout(FlowLayout.RIGHT));
			getContentPane().add(buttonPane, BorderLayout.SOUTH);
			{
				okButton = new JButton("保存");
				okButton.setActionCommand("OK");
				buttonPane.add(okButton);
				getRootPane().setDefaultButton(okButton);
			}
			{
				JButton cancelButton = new JButton("取消");
				cancelButton.setActionCommand("Cancel");
				buttonPane.add(cancelButton);
			}
		}
		
		
		////////////////////////////////////////////////
		okButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				try {
					String s = textField_4.getText();
					// 正则表达式判断
					if(s.matches("\\d{4}-\\d{1,2}-\\d{1,2}") == false) {
						JOptionPane.showMessageDialog(null, "日期格式不正确!");
						return;
					}
					// 字符串转日期
					Date hiredate = Date.valueOf(s);
					
					if(isModify) {
						biz.modify(Integer.parseInt(textField.getText()),
								textField_1.getText(),
								(String)((Map<String,Object>)comboBox.getSelectedItem()).get("JOB"),
								// Oracle 数据库默认返回数字类型为 BigDecimal (大实数)
								((BigDecimal)((Map<String,Object>)comboBox_1.getSelectedItem()).get("EMPNO")).intValue(),
								hiredate,
								Integer.parseInt(""+spinner.getValue()),
								Integer.parseInt(""+spinner_1.getValue()),
								((BigDecimal)((Map<String,Object>)comboBox_1_1.getSelectedItem()).get("DEPTNO")).intValue(),
								passwordField.getText(),
								passwordField_1.getText());
					} else {
						biz.create(textField_1.getText(),
								(String)((Map<String,Object>)comboBox.getSelectedItem()).get("JOB"),
								// Oracle 数据库默认返回数字类型为 BigDecimal (大实数)
								((BigDecimal)((Map<String,Object>)comboBox_1.getSelectedItem()).get("EMPNO")).intValue(),
								hiredate,
								Integer.parseInt(""+spinner.getValue()),
								Integer.parseInt(""+spinner_1.getValue()),
								((BigDecimal)((Map<String,Object>)comboBox_1_1.getSelectedItem()).get("DEPTNO")).intValue(),
								passwordField.getText(),
								passwordField_1.getText());
					}
					JOptionPane.showMessageDialog(null, "员工信息保存成功!");
				} catch (BizException e1) {
					JOptionPane.showMessageDialog(null, e1.getMessage());
				}
			}
		});
	}

	EmpBiz biz = new EmpBiz();
	JButton okButton;
}
